package com.comtime.greendaodemo;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.AbstractDaoSession;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

/**
 * Created by MyPC on 2017/8/8.
 */

public class AdminDao extends AbstractDao<AdminInfo, Long> {
    public static final String TABLENAME = "ADMIN_INFO";

    /**
     * Properties of entity NewInfo.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property aPhoneNum = new Property(1, String.class, "aPhoneNum", false, "PHONE_NUM");
        public final static Property deviceMac = new Property(2, String.class, "deviceMac", false, "DEVICE_MAC");

    };
    public AdminDao(DaoConfig config) {
        super(config);
    }

    public AdminDao(DaoConfig config, AbstractDaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'ADMIN_INFO' (" +
                "'_id' INTEGER PRIMARY KEY ," +
                "'PHONE_NUM' TEXT," +
                "'DEVICE_MAC' TEXT);");
    }
    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'ADMIN_INFO'";
        db.execSQL(sql);
    }


    @Override
    protected AdminInfo readEntity(Cursor cursor, int offset) {
        AdminInfo entity = new AdminInfo(
                cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
                cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // phone
                cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2)); // mac
        return entity;
    }

    @Override
    protected Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }

    @Override
    protected void readEntity(Cursor cursor, AdminInfo entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setPhoneNum(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setMac(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
    }

    @Override
    protected void bindValues(SQLiteStatement stmt, AdminInfo entity) {
        stmt.clearBindings();

        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }

        String phoneNum= entity.getPhoneNum();
        if (phoneNum != null) {
            stmt.bindString(2, phoneNum);
        }

        String deviceMac = entity.getMac();
        if (deviceMac != null) {
            stmt.bindString(3, deviceMac);
        }
    }

    @Override
    protected Long updateKeyAfterInsert(AdminInfo entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }

    @Override
    protected Long getKey(AdminInfo entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    protected boolean isEntityUpdateable() {
        return true;
    }
}
